# -*- coding: utf-8 -*-
import pandas as pd
import os

from otherpkg.utils import timeit


def get_user_action(action, sorted_col, grouped_col, selected_cols):
    action = action.sort_values(sorted_col)
    columns = [grouped_col] + ['his_' + c for c in selected_cols]
    llist = []
    for i, (uid, grouped) in enumerate(action.groupby(grouped_col)):
        row = [uid]
        for j, col in enumerate(selected_cols):
            row.append(' '.join(list(map(str, grouped[col].values.tolist()))))
        llist.append(row)
    result = pd.DataFrame(llist, columns=columns)
    return result


def process_user_action(base_dir, input_filename, output_filename, threshold=13):
    data = pd.read_csv(os.path.join(base_dir, input_filename))
    # click_times最大值处理
    data['click_times'] = data['click_times'].apply(lambda x: threshold if x>threshold else x)
    sorted_col = 'time'
    grouped_col = 'user_id'
    selected_cols = ['time', 'creative_id', 'click_times']
    user_action = get_user_action(data, sorted_col, grouped_col, selected_cols)
    user_action.to_csv(os.path.join(base_dir, output_filename), index=False)


def process_count_features(base_dir, output_filename):
    train_dir = base_dir + '/train_preliminary'
    train_click_log = pd.read_csv(train_dir + '/click_log.csv')
    train_user = pd.read_csv(train_dir + '/user.csv')
    train_data = pd.merge(train_click_log, train_user, on='user_id')
    res = []
    index = 'creative_id'
    for c in ['age', 'gender']:
        data = train_data.copy()
        data[c] = data[c].map(lambda x: c + '_' + str(x))
        data['value'] = 1
        age_count = data.pivot_table(index=index, columns=c, values='value', aggfunc='sum', fill_value=0)
        age_count.reset_index(inplace=True)
        res.append(age_count)
    overall_count = pd.merge(res[0], res[1], on=index)
    overall_count.to_csv(os.path.join(base_dir, output_filename), index=False)


@timeit
def main(base_dir):
    for flag in range(5):
        process_user_action(base_dir, 'train_{}.csv'.format(flag), 'train_user_action_{}.csv'.format(flag))
        process_user_action(base_dir, 'valid_{}.csv'.format(flag), 'valid_user_action_{}.csv'.format(flag))
    process_user_action(base_dir, 'test/click_log.csv', 'test_user_action.csv')

    # 处理统计特征
    # process_count_features(base_dir, 'count_feature.csv')


if __name__ == "__main__":
    base_dir = './input'
    # base_dir = './sample'
    main(base_dir)
